<?php

namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Admin\Model\Bangdiem;
use Admin\Form\BangdiemForm;
use Zend\Db\Sql\Select;
use Zend\Paginator\Paginator;
use Zend\Paginator\Adapter\Iterator as paginatorIterator;
use Zend\Session\Container;

class BangdiemController extends AbstractActionController {

    protected $bangdiemTable;

	public function kiemtra(){
		$module = "bangdiem";
		$controller = $this->params('controller');
		$action = $this->params('action');
		
		$sessionModule = new Container('module');
		$sessionController = new Container('controller');
		$sessionAction = new Container('action');
		$sessionModule->lastModule = $module;
		$sessionController->lastController = $controller;
		$sessionAction->lastAction = $action;
		
		$sessionUser = new Container('user');
		if(!$sessionUser->tendangnhap){
			return $this->redirect()->toRoute('auth');
		}
		else if($sessionUser->tendangnhap && $sessionUser->quyen != 1){
			return $this->redirect()->toRoute('home');
		}
	}
    public function indexAction() {
		
		$this->kiemtra();
		$request = $this->getRequest();
		if ($request->isPost()) {
			$order_by = $request->getPost('order_by');
			$order = $request->getPost('order');
			$page = $request->getPost('page');
			$keyword = $request->getPost('keyword');
			$limit = $request->getPost('limit');
		}
		else{
			$order_by = $this->params()->fromRoute('order_by') ?
                $this->params()->fromRoute('order_by') : 'id';
			$order = $this->params()->fromRoute('order') ?
                $this->params()->fromRoute('order') : Select::ORDER_DESCENDING;
        	$page = $this->params()->fromRoute('page') ? (int) $this->params()->fromRoute('page') : 1;
			$keyword = "";
			$limit = "";
		}
		
        $select = new Select();

       

		if($limit=="" && $keyword!="")
				$bangdiems = $this->getBangdiemTable()->fetchAll($select->where('tenmonhoc like "%'.$keyword.'%" OR tenhs like "%'.$keyword.'%" OR sodiem like "%'.$keyword.'%" OR namhoc like "%'.$keyword.'%" OR hocki like "%'.$keyword.'%" ')->order($order_by . ' ' . $order));
		else if($limit!="")
		{
			if($keyword!="")
				$bangdiems = $this->getBangdiemTable()->fetchAll($select->where('tenmonhoc like "%'.$keyword.'%" OR tenhs like "%'.$keyword.'%" OR sodiem like "%'.$keyword.'%" OR namhoc like "%'.$keyword.'%" OR hocki like "%'.$keyword.'%" ')->order($order_by . ' ' . $order)->limit($limit));
			else 
				$bangdiems = $this->getBangdiemTable()->fetchAll($select->order($order_by . ' ' . $order)->limit($limit));
		}
		else
        	$bangdiems = $this->getBangdiemTable()->fetchAll($select->order($order_by . ' ' . $order));
        $itemsPerPage = 10;

        $bangdiems->current();
        $paginator = new Paginator(new paginatorIterator($bangdiems));
        $paginator->setCurrentPageNumber($page)
                ->setItemCountPerPage($itemsPerPage)
                ->setPageRange(7);

        return new ViewModel(array(
                    'order_by' => $order_by,
                    'order' => $order,
                    'page' => $page,
                    'paginator' => $paginator,
					'keyword' => $keyword,
                ));
    }

    public function addAction() {
		
		$this->kiemtra();
		
		$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
        $form = new BangdiemForm($dbAdapter);
        $form->get('submit')->setAttribute('value', 'Thêm');
		$viewModel = new ViewModel();
        $request = $this->getRequest();
        if ($request->isPost()) {
            $bangdiem = new Bangdiem();
            $form->setInputFilter($bangdiem->getInputFilter());
            $form->setData($request->getPost());
            if ($form->isValid()) {
                $bangdiem->exchangeArray($form->getData());
				
					$this->getBangdiemTable()->saveBangdiem($bangdiem);
	
					// Redirect to list of bangdiems
					return $this->redirect()->toRoute('bangdiem');
				
            }
        }

        return array('form' => $form);
    }

    public function editAction() {
		$this->kiemtra();
		
        $id = (int) $this->params('id');
        if (!$id) {
            return $this->redirect()->toRoute('bangdiem', array('action' => 'add'));
        }
		$viewModel = new ViewModel();
        $bangdiem = $this->getBangdiemTable()->getBangdiem($id);
		if (!$bangdiem) {
            return $this->redirect()->toRoute('bangdiem');
        }

		$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
        $form = new BangdiemForm($dbAdapter);
        $form->bind($bangdiem);
        $form->get('submit')->setAttribute('value', 'Sửa');
		

        $request = $this->getRequest();
        if ($request->isPost()) {
            $form->setData($request->getPost());
            if ($form->isValid()) {
				
                	$this->getBangdiemTable()->saveBangdiem($bangdiem);

					// Redirect to list of bangdiems
					return $this->redirect()->toRoute('bangdiem');
				
            }
        }

        return array(
            'id' => $id,
            'form' => $form,
        );
    }

   public function deleteAction() {
		$this->kiemtra();
		
		$request = $this->getRequest();
		if ($request->isPost()) {
			$id = $request->getPost('data');
			if (!$id) {
            	return $this->redirect()->toRoute('bangdiem');
        	}
			else{
				$this->getBangdiemTable()->deleteBangdiem($id);
				// Redirect to list of users
				return $this->redirect()->toRoute('bangdiem');
			}
		}

        return array(
            'id' => $id,
            'bangdiem' => $this->getBangdiemTable()->getBangdiem($id)
        );
    }
	public function updateAction() {
		$this->kiemtra();
		
		$request = $this->getRequest();
		if ($request->isPost()) {
			$xoa = $request->getPost('xoa');
			
			if ($xoa!="") {
            	$this->getBangdiemTable()->xoaBangdiem($xoa);
				return $this->redirect()->toRoute('bangdiem');
        	}
			
			else {
            	return $this->redirect()->toRoute('bangdiem');
        	}
		}

        
    }


    public function getBangdiemTable() {
        if (!$this->bangdiemTable) {
            $sm = $this->getServiceLocator();
            $this->bangdiemTable = $sm->get('Admin\Model\BangdiemTable');
        }
        return $this->bangdiemTable;
    }

}
